package com.example.shiwu.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.shiwu.ElectrodeRoDetailsActivity;
import com.example.shiwu.R;
import com.example.shiwu.SendReceiveImgDetailsActivity;
import com.example.shiwu.SendReceiveListActivity;
import com.example.shiwu.db.model.BoxElectrodeRo;
import com.example.shiwu.db.model.Items;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.db.vo.ElectrodeRoVo;
import com.example.shiwu.service.BoxElectrodeRoService;
import com.example.shiwu.service.ItemsService;
import com.example.shiwu.service.SendsService;
import com.example.shiwu.util.DateUtils;
import com.example.shiwu.util.FileDownUtil;

import java.util.List;

public class ElectrodeRoAdapter extends BaseAdapter implements View.OnClickListener{

    private SendReceiveListActivity mContext;
    private int resourceId;
    public boolean flage = false;
    private List<ElectrodeRoVo> listItems;
    private LayoutInflater mInflater;
    private String[] myModaData,freqData,freqFilterData, repeatData, valtageData;
    private Integer checkBoxIDList;            //存储checkBox的值
    private MyClickListener mListener,rListener;
    private ItemsService itemsService;
    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener{
        public void clickListener(View v);
        boolean clickRemoveListener(int position);
    }



    public ElectrodeRoAdapter(SendReceiveListActivity mContext, List<ElectrodeRoVo> listItems, int resource, String[] modeData,
                              String[] freqData, String[] repeatData, String[] valtageData, MyClickListener listener) {
        this.resourceId = resource;
        this.mContext = mContext;
        this.listItems = listItems;
        this.myModaData = modeData;
        this.freqData = freqData;

        this.repeatData = repeatData;
        this.valtageData = valtageData;
        this.mListener = listener;
        this.rListener = listener;
        itemsService = new ItemsService();
    }
    public void cleckAll() {
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        final ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder(view, position);
            viewHolder.ele_did.setText(listItems.get(position).getId());

            viewHolder.device_id.setText(listItems.get(position).getDeviceId());
            viewHolder.ele_items_name.setText(listItems.get(position).getName());
            viewHolder.ele_operator.setText(String.valueOf(listItems.get(position).getOperator()));
            viewHolder.ele_items_voltage.setText(String.valueOf(listItems.get(position).getVoltage())+"V");
            viewHolder.ele_items_createTime.setText(listItems.get(position).getCreateTime());
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ElectrodeRoAdapter.ViewHolder) view.getTag();
            viewHolder.ele_did.setText(listItems.get(position).getId());
            viewHolder.device_id.setText(listItems.get(position).getDeviceId());
            viewHolder.ele_items_name.setText(listItems.get(position).getName());
            viewHolder.ele_operator.setText(String.valueOf(listItems.get(position).getOperator()));
            viewHolder.ele_items_voltage.setText(String.valueOf(listItems.get(position).getVoltage())+"V");
            viewHolder.ele_items_createTime.setText(listItems.get(position).getCreateTime());
        }


        viewHolder.detailsSub.setOnClickListener(new View.OnClickListener() {//详情
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ElectrodeRoDetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Integer sId = Integer.parseInt(listItems.get(position).getId());
                Integer itemId = listItems.get(position).getItemId();
                intent.putExtra("itemId",String.valueOf(itemId));
                mContext.startActivity(intent);
            }
        });

        viewHolder.delSb.setOnClickListener(new View.OnClickListener() {//删除
            @Override
            public void onClick(View v) {
                System.out.println("id =================================== " + v.getId());
                //点击删除按钮之后，给出dialog提示
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("确认删除?");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("id =================================== " + position);
                        System.out.println("——————————————删除——————————————————");
                        Integer sId = Integer.parseInt(listItems.get(position).getId());
                        String createTime =listItems.get(position).getCreateTime();
                        Integer itemId = listItems.get(position).getItemId();

                        Items items = itemsService.getItems(new Items(itemId)).get(0);

                        BoxElectrodeRo sen1 = new BoxElectrodeRo();
                        sen1.setStatus(1);
                        sen1.setItemId(String.valueOf(itemId));
                        BoxElectrodeRoService boxElectrodeRoService = new BoxElectrodeRoService();
                        BoxElectrodeRo sen2 = new BoxElectrodeRo();
                        sen2.setId(sId);
                        Boolean bool = boxElectrodeRoService.updade(sen1, sen2);//先删表数据在删除文件
                        //Boolean bool = sendService.delete(new Sends(itemId));
                        if (bool) {
                            Toast.makeText(v.getContext(), "删除成功！", Toast.LENGTH_SHORT).show();

                        }


                    }
                });
                builder.show();
            }
        });
        //获取复选框选中状态改变事件进行增删改
        viewHolder.ele_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                /*
                 * b=选中状态
                 * if b = true 将值添加至checkBoxIDList
                 * if b = false 将值从checkBoxIDList移除
                 * */
                Integer itemId = Integer.parseInt(listItems.get(position).getId());
                if (b) {

                    checkBoxIDList = itemId;

                } else {

                    checkBoxIDList = -itemId;

                }
                System.out.println(checkBoxIDList);
                compoundButton.setId(checkBoxIDList);
                mListener.clickListener(compoundButton);
            }
        });

        return view;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    //响应按钮点击事件,调用子定义接口，并传入View
    @Override
    public void onClick(View v) {
        mListener.clickListener(v);
    }
    class ViewHolder {

        public TextView ele_did,detailsSub,delSb, device_id,ele_items_name, ele_items_voltage,ele_operator,ele_items_createTime;
        public CheckBox ele_checkbox;

        public ViewHolder(View view, int position) {
            ele_did = view.findViewById(R.id.ele_did);
            device_id = view.findViewById(R.id.device_id_val);
            ele_items_name = view.findViewById(R.id.ele_items_name_val);
            ele_items_voltage = view.findViewById(R.id.ele_items_voltage_val);
            ele_operator = view.findViewById(R.id.ele_operator_val);
            ele_items_createTime = view.findViewById(R.id.ele_items_createTime_val);
            detailsSub = view.findViewById(R.id.ele_items_details_sb);
            delSb = view.findViewById(R.id.ele_items_del_sb);
            ele_checkbox = view.findViewById(R.id.ele_list_items_checkbox);
        }

    }

}
